import {createApp} from 'vue'
import Toast from './index.vue'

const showToast = (msg, options = {duration: 1500, type: ''}) => {
    const {duration, type} = options
    const div = document.createElement('div')
    div.style.position = 'relative'
    div.style.zIndex = 99999
    const componentInstance = createApp(Toast, {
        show: true,
        msg,
        duration,
        type,
    })

    componentInstance.mount(div)
    document.body.appendChild(div)

    let timer = null
    clearTimeout(timer)
    timer = setTimeout(() => {
        componentInstance.unmount(div)
        document.body.removeChild(div)
    }, duration)
}

// export default showToast

export default {
    error(msg) {
        showToast(msg, {type: 'error'})
    },
    success(msg) {
        showToast(msg, {type: 'success'})
    },
    warning(msg) {
        showToast(msg, {type: 'warning'})
    }
}